<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property string $id
 * @property string $category_id
 * @property string $name
 * @property string $name_en
 * @property string $url_key
 * @property string $last_modified_time
 * @property string $title
 * @property string $title_en
 * @property string $content
 * @property string $content_en
 * @property integer $view_count
 * @property string $created_at
 * @property string $price
 * @property string $price_en
 * @property integer $index
 * @property string $promotion_price
 * @property string $promotion_price_en
 *
 * The followings are the available model relations:
 * @property Images[] $images
 * @property Category $category
 */
class Product extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category_id, name, url_key, last_modified_time', 'required'),
			array('view_count, index', 'numerical', 'integerOnly'=>true),
			array('category_id', 'length', 'max'=>11),
			array('name, name_en, url_key, price, price_en, promotion_price, promotion_price_en', 'length', 'max'=>255),
			array('title, title_en, content, content_en, created_at', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, category_id, name, name_en, url_key, last_modified_time, title, title_en, content, content_en, view_count, created_at, price, price_en, index, promotion_price, promotion_price_en', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'images' => array(self::HAS_MANY, 'Images', 'product_id'),
			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category_id' => 'Category',
			'name' => 'Name',
			'name_en' => 'Name En',
			'url_key' => 'Url Key',
			'last_modified_time' => 'Last Modified Time',
			'title' => 'Title',
			'title_en' => 'Title En',
			'content' => 'Content',
			'content_en' => 'Content En',
			'view_count' => 'View Count',
			'created_at' => 'Created At',
			'price' => 'Price',
			'price_en' => 'Price En',
			'index' => 'Index',
			'promotion_price' => 'Promotion Price',
			'promotion_price_en' => 'Promotion Price En',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('category_id',$this->category_id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('name_en',$this->name_en,true);
		$criteria->compare('url_key',$this->url_key,true);
		$criteria->compare('last_modified_time',$this->last_modified_time,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('title_en',$this->title_en,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('content_en',$this->content_en,true);
		$criteria->compare('view_count',$this->view_count);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('price_en',$this->price_en,true);
		$criteria->compare('index',$this->index);
		$criteria->compare('promotion_price',$this->promotion_price,true);
		$criteria->compare('promotion_price_en',$this->promotion_price_en,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function getPathImage($id)
	{
		$model=Product::model()->findByPk($id);

		foreach($model->images as $image){
			return $image;
			break;
		}
	}
}